#coding= utf-8
from HTMLTestRunner import HTMLTestRunner # 用例运行器，用于生成测试报告
import smtplib
import  unittest
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class ems:
    def em(self):
        # 扫描6条用例
        tests = unittest.defaultTestLoader.discover(r"D:\py\邮件\邮件", pattern="Test*.py")

        # 创建运行器
        runner = HTMLTestRunner.HTMLTestRunner(
            title="这是计算器测试报告",
            description="这是加法测试报告",
            verbosity=1,
            stream=open(file="Calc.html", mode="w+", encoding="utf-8")
        )
        # 让运行器运行6条用例
        runner.run(tests)
    def ems(self):
        # 实例化邮件
        msg = MIMEMultipart()
        # 发送服务器
        msg_server="smtp.qq.com"
        # 发送方地址
        msg_from = "1435015358@qq.com"
        # 授权码
        passwd = "fponbuptittpbahb"
        # 收件地址
        to ="2431320433@qq.com"
        # 设置邮件主题
        subject="测试报告"
        #文件内容
        Text = "姓名：姚州庆"
        msg.attach(MIMEText(Text, 'plain', 'utf-8'))
        msg['subject'] = subject
        msg['from'] = msg_from
        msg['to'] = to
        # 构造附件内容（格式为html)
        # MIMEText(内容，文本格式，编码   open(路径，'r'）
        # f = open("gp.txt",encoding='utf-8').read()  //报错点
        file = MIMEText(open("D:\\py\\邮件\邮件\\Calc.html", encoding='utf-8').read(), 'base64',
                        'utf-8')
        file['Content-Type'] = 'application/octet-stream'
        file.add_header('Content-Disposition', 'attachment', filename="计算器报告.html")
        #附件加到邮件中
        msg.attach(file)
        smtp = smtplib.SMTP()
        #获取发送服务器
        smtp.connect(msg_server)
        print("连接邮箱服务器成功")
        #登录服务器
        smtp.login(msg_from,passwd)
        print("登录成功")
        #发送邮件sendmail(发件人，收件人，字符串格式的正文)
        smtp.sendmail(msg_from,to,msg.as_string())
        print("发送成功")
        #退出smtp
        smtp.quit()
        print("退出")
tx=ems()
tx.ems()
tx.em()

















